using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class Security
{
	private static readonly string secret = "QJFNuU5K9xHN1Oit6l70J547vUyv7I6y";

	public static string DecryptStringAES(string cipherText)
	{
		byte[] bytes = Encoding.UTF8.GetBytes(secret);
		byte[] bytes2 = Encoding.UTF8.GetBytes(secret.Substring(0, 16));
		byte[] cipherText2 = Convert.FromBase64String(cipherText);
		return DecryptStringFromBytes(cipherText2, bytes, bytes2);
	}

	public static string DecryptStringAES(string cipherText, string key)
	{
		byte[] bytes = Encoding.UTF8.GetBytes(key);
		byte[] bytes2 = Encoding.UTF8.GetBytes(key.Substring(0, 16));
		byte[] cipherText2 = Convert.FromBase64String(cipherText);
		return DecryptStringFromBytes(cipherText2, bytes, bytes2);
	}

	public static string EncryptStringAES(string plainText)
	{
		byte[] bytes = Encoding.UTF8.GetBytes(secret);
		byte[] bytes2 = Encoding.UTF8.GetBytes(secret.Substring(0, 16));
		byte[] inArray = EncryptStringToBytes(plainText, bytes, bytes2);
		return Convert.ToBase64String(inArray);
	}

	public static string EncryptStringAES(string plainText, string key)
	{
		byte[] bytes = Encoding.UTF8.GetBytes(key);
		byte[] bytes2 = Encoding.UTF8.GetBytes(key.Substring(0, 16));
		byte[] inArray = EncryptStringToBytes(plainText, bytes, bytes2);
		return Convert.ToBase64String(inArray);
	}

	public static byte[] EncryptStringAESToBytes(string plainText)
	{
		byte[] bytes = Encoding.UTF8.GetBytes(secret);
		byte[] bytes2 = Encoding.UTF8.GetBytes(secret.Substring(0, 16));
		return EncryptStringToBytes(plainText, bytes, bytes2);
	}

	private static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
	{
		if (plainText == null || plainText.Length <= 0)
		{
			throw new ArgumentNullException("plainText");
		}
		if (key == null || key.Length <= 0)
		{
			throw new ArgumentNullException("key");
		}
		if (iv == null || iv.Length <= 0)
		{
			throw new ArgumentNullException("iv");
		}
		using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
		{
			rijndaelManaged.Mode = CipherMode.CBC;
			rijndaelManaged.Padding = PaddingMode.PKCS7;
			rijndaelManaged.KeySize = 256;
			rijndaelManaged.BlockSize = 128;
			rijndaelManaged.Key = key;
			rijndaelManaged.IV = iv;
			ICryptoTransform transform = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
				{
					using (StreamWriter streamWriter = new StreamWriter(stream))
					{
						streamWriter.Write(plainText);
					}
					return memoryStream.ToArray();
				}
			}
		}
	}

	private static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
	{
		if (cipherText == null || cipherText.Length <= 0)
		{
			throw new ArgumentNullException("cipherText");
		}
		if (key == null || key.Length <= 0)
		{
			throw new ArgumentNullException("key");
		}
		if (iv == null || iv.Length <= 0)
		{
			throw new ArgumentNullException("iv");
		}
		string result = null;
		using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
		{
			rijndaelManaged.Mode = CipherMode.CBC;
			rijndaelManaged.Padding = PaddingMode.PKCS7;
			rijndaelManaged.KeySize = 256;
			rijndaelManaged.BlockSize = 128;
			rijndaelManaged.Key = key;
			rijndaelManaged.IV = iv;
			ICryptoTransform transform = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
			try
			{
				using (MemoryStream stream = new MemoryStream(cipherText))
				{
					using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
					{
						using (StreamReader streamReader = new StreamReader(stream2))
						{
							result = streamReader.ReadToEnd();
						}
					}
				}
			}
			catch
			{
				result = "keyError";
			}
		}
		return result;
	}
}
